package org.ruiyichen.guizhou.agr.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseFragment;
import org.ruiyichen.guizhou.agr.base.DataManger;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.bean.mode.CollectDeviceModel;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.imp.CollectDeviceFilter;
import org.ruiyichen.guizhou.agr.imp.IIrrigatedAreaFilter;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.activity.HomeActivity;
import org.ruiyichen.guizhou.agr.ui.adapter.CollectDeviceRecyclerAdapter;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.ListUtil;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 采集设备
 */
public class CollectDeviceFragment extends BaseFragment implements View.OnClickListener {


    private RecyclerView rvSupportRoot;

    //设备采集数据列表
    private CollectDeviceRecyclerAdapter adapter;
    //设备采集列表
    private List<CollectDeviceModel> list = new ArrayList<>();

    @Override
    public void onResume() {
        super.onResume();
        if (!getUserVisibleHint()) {
            return;
        }
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            bHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    initData();
                }
            }, 500);
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.df_fragment_collect_device, container, false);
            bOkHttpManager = new OkHttpManager(getActivity(), bHandler);
            initViews();
            initEvents();
            initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    private void initViews() {

        rvSupportRoot = (RecyclerView) mRootView.findViewById(R.id.rv_support_root);
        LinearLayoutManager manager = new LinearLayoutManager(getCustomActivity());
        manager.setAutoMeasureEnabled(true);
        rvSupportRoot.setLayoutManager(manager);
        rvSupportRoot.setNestedScrollingEnabled(false);


        list.add(new CollectDeviceModel("采集设备报送数据"));

        adapter = new CollectDeviceRecyclerAdapter(getCustomActivity());
        adapter.setmDatas(list);
        rvSupportRoot.setAdapter(adapter);

    }


    private void initEvents() {

    }

    @Override
    public boolean onBackPressed() {
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_normal_other:
                break;
        }
    }

    private HomeActivity getCustomActivity() {
        return (HomeActivity) getActivity();
    }

    private void initData() {
        Map<String, String> body = new HashMap<>();
        bOkHttpManager.httpRequest(Constant.RequestType.SELECT_STATE_ALL, Constant.Http.GET, body,
                false, new
                TypeToken<Response<List<CollectDeviceModel>, Void>>() {
                }.getType(), true, false);
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
            } else {
                if (requestType == Constant.RequestType.SELECT_STATE_ALL) {
                    Response<List<CollectDeviceModel>, Void> result = (Response<List<CollectDeviceModel>, Void>) response;
                    if (result.isSuccess()) {
                        list.clear();
                        if (ListUtil.isNotEmpty(result.getData())) {
                            list.addAll(result.getData());
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        BToast.getInstance().show(result.getMsg());
                    }
                }
            }
        }
    }
}

